// packageA/withdraw/withdraw.js

import {
  userDetail,
  cashoutAdd
} from '../../common/api/my'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "", // 支付宝名字
    account: "", // 支付宝账号
    user: {}, // 用户提现金额
    money: "", // 提现金额
    checked:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    let user = wx.getStorageSync('userInfo');
    console.log(e);
    return {
      title: "杆赢天下，求胜未来。一元乐享好球杆，千万球友的选择",
      imageUrl: "../../images/shareImg.jpg",
       path: "/pages/index/index?id=" + user.id
    }
  },


  async getUserDetail() {
    let user = wx.getStorageSync('userInfo');
    let res = await userDetail({
      id: user.id
    });
    console.log(res);
    this.setData({
      user: res
    })
  },
  /***
   * 
   * 
   */
  onInputChange(e){
    this.data.account = e.detail.value;
  
  },
  onInputChange2(e){
    this.data.username = e.detail.value;
  },
  onInputChange3(e){
    this.data.money = e.detail.value;
  },
  /**
   * 提交
   * **/ 
  async withdraw(){
    
    if(this.data.money == 0){
      wx.showToast({
        title: '提现金额不能为0',
          icon: "none",
      })
      return
    }else if(!this.data.account){
      wx.showToast({
        title: '请输入支付宝账号',
          icon: "none",
      })
      return
    }else if(!this.data.username){
      wx.showToast({
        title: '请输入真实姓名',
          icon: "none",
      })
      return
    }

   let user = wx.getStorageSync('userInfo');;
    let data = {
      userId: user.id,
      money: this.data.money,
      account: this.data.account,
      username: this.data.username
    };
    let res = await cashoutAdd(data);
    let vm = this;
    if(res == null){
      wx.showToast({
        title: '提现成功',
          icon: "none",
          success(){
            setTimeout(() => {
              // wx.navigateBack({
              //   delta: 1
              // })
              vm.goList();
            }, 1.5 * 1000);
          }
      })
    }else {
    wx.showToast({
      title: res.message,
        icon: "none"
    })
    }
    console.log(res)
  },
  // 跳转提现列表
  goList(){
      wx.navigateTo({
        url: '/packageA/withdrawList/withdrawList',
      })
  },
  checkedTap: function () {
    this.setData({
      "checked": !this.data.checked
    })
  },
})